var packModelTable = function() {
	return {
		init : function() {
			
			util.getDictByParentKey("IS_ENABLE_FLAG", 'form_enableFlag', '请选择');
			$("#form_ftpParamsId").select2({
	          	     placeholder:'请选择',//默认文字提示
	          	     language: "zh-CN",//汉化
	          	     allowClear: true//允许清空
	          	});
			$("#form_colClientId").select2({
					placeholder:'请选择',//默认文字提示
					language: "zh-CN",//汉化
					allowClear: true//允许清空
				});
				
			var validator;
			var tableId = "client_params_table";
			//初始化checkBox 初始化行点击选中
			util.initCheckBox(tableId);
			function queryData(sSource, aoData, fnCallback) {
				aoData.push({
					"name" : "colClientId",
					"value" : $("#id_client").val().trim()
				});
			
				$.ajax({
					"type" : "post",
					"url" : sSource,
					"dataType" : "json",
					"data" : {
						"aoData" : JSON.stringify(aoData)
					},
					"success" : function(resp) {
						fnCallback(resp);
					}
				});
			};
			$('#client_params_table').DataTable({
				"processing" : true,
				"serverSide" : true,
				"sAjaxSource" : "params/client/getClientParamsList.mvc",
				"fnServerData" : queryData,
				"pageLength" : 10,
				"sDom" : "<'row-fluid'r>t<'row-fluid'<'span6'i><'span6'p>>",
				"oLanguage" : util.getDToLanguage(),
				"columns" : [ {
					"data" : "pkId",
					"render" : function(data, type, full, meta) {
						 return '<div class="checker" onclick="util.selectCheckBox(\'' + tableId + '\', this)"><span><input type="checkbox" class="checkboxes"/></span></div>';
					},
					"bSortable" : false
				},
				{
					"data" : "ftpParamsId",
					"bSortable" : false,
					"defaultContent": ""
				}, 
				{
					"data" : "colClientId",
					"bSortable" : false,
					"defaultContent": ""
				},
				{
					"data" : "isSend",
					"bSortable" : false,
					"defaultContent": ""
				},
				{
					"data" : "enableFlag",
					"bSortable" : false,
					"defaultContent": ""
				},
				{
					"data" : "updateDateStr",
					"bSortable" : false,
					"defaultContent": ""
				}],
				"fnDrawCallback" : function(oSetting) {
					util.clearSelected(tableId);
				}
			});
			
			//窗口关闭时重置内容
			$("#form_btn_close").click(function(e) {
				$("#form_packModelId").val("");
				$("#div_ftp_params_form")[0].reset();
				if(validator) {
					$("#div_ftp_params_form").validate().resetForm();
				}
			});
			
			// 查询
			$("#btn_query").click(function(e) {
				var table = $('#client_params_table').DataTable();
				table.draw(true);
			});
			
		    // 重置
			$("#btn_reset").click(function(e) {
				$("#ftp_params_form")[0].reset();
			});
			
			//新增
			$("#client_params_add").click(function(e){
				$('#addEmploy_title').text('新增参数配置');
				util.getOption("params/client/getFtpParamsOptions.mvc", null, "form_ftpParamsId");
				util.getOption("params/client/getClientOptions.mvc", null, "form_colClientId");
				$("#add_employ").modal({show:true,keyboard:false});
			});
			
			// 修改
			$('#client_params_edit').on('click', function(e) {
				var d = [];
				d = util.getCheckedData("client_params_table");
				if (d.length != 1) {
					util.showModal('#_alertModal', '请选择一条记录进行操作！')
					return false;
				}
				$('#addEmploy_title').text('编辑参数配置');
				util.getOption("params/client/getFtpParamsOptions.mvc", null, "form_ftpParamsId");
				util.getOption("params/client/getClientOptions.mvc", null, "form_colClientId");
				$.ajax({
					"type" : "post",
					"url" : "params/client/getFTPParamsById.mvc",
					"dataType" : "json",
					"data" : {
						"pkId" : d[0].pkId
					},
					"success" : function(resp) {
						if (resp) {
							$("#form_ftpParamsId").val(resp.ftpParamsId);
							$("#form_ftpParamsId").change();
							$("#form_colClientId").val(resp.colClientId);
							$("#form_colClientId").change();
							$("#form_enableFlag").val(resp.enableFlag);
							$("#form_pkId").val(resp.pkId);
							$("#add_employ").modal({show:true,keyboard:false});
						} else {
							rmsBox.notify('error', {msg: '数据异常！'});
							return false;
						}
					},
					"error" : function() {
						rmsBox.notify('error', {msg: '数据异常！'});
						return false;
					}
				});
			});
			
			//删除
			$("#client_params_del").on('click', function(){
				var d = [];
				d = util.getCheckedData("client_params_table");
				if (d.length == 0) {
					util.showModal('#_alertModal', '请选择要操作的记录！')
				} else {
					util.showModal('#_confirmModal', '您确定要删除选中记录？','_confirmModal_yes')
				}
			});
			
			//确认删除
			$("#_confirmModal").on('click', '#_confirmModal_yes', function(){
				var d = [];
				d = util.getCheckedData("client_params_table");
				var idArray=[];
				for (var i=0;i<d.length;i++) {
					idArray.push(d[i].pkId);
				}
				var table = $('#client_params_table').DataTable();
				$.ajax({
					"type" : "post",
					"url" : "params/client/delete.mvc",
					"dataType" : "json",
					"data" : {
						"pkId" : idArray.join(",")
					},
					"success" : function(resp) {
							rmsBox.notify('success', {msg: '删除成功！'});
							table.draw(true);
					},
					"error":function(){
						rmsBox.notify('error', {msg: '操作失败！',delay:3000});
					}
				});
			});
			
			
			$(function(){
				$('.search-body .my-input').bind('keyup', function(event) {
					if (event.keyCode == "13") {
						//回车执行查询
						$('.search-button #btn_query').click();
					}
				});
			});
			
			//保存
			$("#form_model_save").click(function(){
				validator = validateMyForm("div_ftp_params_form");
				if(!validator.form()) {
					return false;
				}
				var data = {};
				data['pkId'] = $("#form_pkId").val();
				data['ftpParamsId'] = $("#form_ftpParamsId").val();
				data['colClientId'] = $("#form_colClientId").val();
				data['enableFlag'] = $("#form_enableFlag").val();
				data['isSend'] = '0';
				var table = $('#client_params_table').DataTable();
				$.ajax({
					"type" : "post",
					"url" : "params/client/saveOrUpdate.mvc",
					"dataType" : "json",
					"data" : data,
					"success" : function(resp) {
							rmsBox.notify('success', {msg: '保存成功！'});
							$("#form_btn_close").click();
							table.draw(true);
					},
					"error" : function(resp) {
						rmsBox.notify('error', {msg: '保存失败！',delay:3000});
					}
				});
			});
			
			function validateMyForm(formId){
				return $( '#' + formId ).validate({
					errorElement: 'em',
					focusInvalid: false, 
					ignore: "",
			        rules:{  
			        	ftpParamsId:'required',
			        	colClientId:'required',
			        	enableFlag:'required'
			        },
			        messages:{  
			        	ftpParamsId:'请选择',
			        	colClientId:'请选择',
			        	enableFlag:'请选择'
			        }, 
			        errorPlacement: function (error, element) { 
			        	//默认以span形式显示在输入框后
			           error.insertAfter(element);
			           $(element.next()).attr('title', $(error).text()).tooltip();
			           $(error).text('');
			        }
			    });
			}
		}
	};
}();

